﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using EntityInterfaces;

namespace DAL
{
    public class Category : ICategory
    {
        private shopnetEntities model;
        private category entity;


        public int id {
            get {
                return (int)entity.categoryID;
            }
        }

        public String name
        {
            get
            {
                return (String)entity.name;
            }
            set
            {
                entity.name = value;
            }
        }

        public int parent
        {
            get
            {
                return (int)entity.parent;
            }
            set {
                entity.parent = value;
            }
        }

        public int products_count
        {
            get
            {
                return (int)entity.products_count;
            }
        }

        public String description
        {
            get
            {
                return (String)entity.description;
            }
            set
            {
                entity.description = value;
            }
        }
        public String picture
        {
            get
            {
                return (String)entity.picture;
            }
            set
            {
                entity.picture = value;
            }
        }

        public int products_count_admin
        {
            get
            {
                return (int)entity.products_count;
            }
        }

        public Category() {
            model = new shopnetEntities();
        }

        public void Save()
        {
            model.categories.AddObject(entity);
            try
            {
                model.SaveChanges();
            }
            catch (Exception exception)
            {
                throw new EntityException.CategoryException("Category Save error", exception);
                // log

            }
        }
        public void Read(int id)
        {
            try
            {
                entity = model.categories.Single(s=> s.categoryID == id);
            }
            catch (Exception exception)
            {
                throw new EntityException.CategoryException("Category Read error", exception);
                //log
            }
        }
        public void Commit()
        {
            try
            {
                model.SaveChanges();
            }
            catch (Exception exception)
            {
                //log
                throw new EntityException.CategoryException("Category Commit error", exception);
            }
        }

        public List<ICategory> GetSubCategories() { 
            var list = new List<ICategory>();
            try
            {
              var tmp = model.categories.Where(c => c.parent == id);
            foreach (var i in tmp) {
                var sub = new Category();
                sub.Read(i.categoryID);
                list.Add(sub);
            }
            }
            catch (Exception exception)
            {
                //log
                throw new EntityException.CategoryException("Category GetSubCategories error", exception);
            }
            return list;
        }

        public List<IProduct> GetProducts()
        {
            var list = new List<IProduct>();
            try
            {
                var tmp = model.products.Where(p => p.categoryID == id);
                foreach (var i in tmp)
                {
                    var prd = new Product();
                    prd.Read(i.productID);
                    list.Add(prd);
                }
            }
            catch (Exception exception)
            {
                //log
                throw new EntityException.CategoryException("Category GetProducts error", exception);
            }
            return list;
        }
    }
}
